﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SBToolkit.Collections
{
    public class CollectionComparer<TType>
    {
        private readonly IEnumerable<TType> original;

        private readonly IEnumerable<TType> current;

        private readonly IEqualityComparer<TType> comparer;

        public CollectionComparer(IEnumerable<TType> original, IEnumerable<TType> current)
        {
            if (original == null)
            {
                throw new ArgumentNullException("original");
            }

            if (current == null)
            {
                throw new ArgumentNullException("current");
            }

            this.original = original;
            this.current = current;
        }

        public CollectionComparer(IEnumerable<TType> original, IEnumerable<TType> current, IEqualityComparer<TType> comparer)
            : this(original, current)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            this.comparer = comparer;
        }

        public IEnumerable<TType> Added
        {
            get
            {
                return comparer == null ? current.Except(original) : current.Except(original, comparer);
            }
        }

        public IEnumerable<TType> Removed
        {
            get
            {
                return comparer == null ? original.Except(current) : original.Except(current, comparer);
            }
        }

        public IEnumerable<TType> UpdatedOrUnchanged
        {
            get
            {
                return comparer == null ? current.Except(Added) : current.Except(Added, comparer);
            }
        }
    }
}
